<%--
  Created by IntelliJ IDEA.
  User: 21553
  Date: 2021/10/31
  Time: 16:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<meta http-equiv="refresh" content="1">
<head>
    <title>Title</title>
</head>
<body>
<%
    request.setCharacterEncoding("UTF-8");
    String user = request.getParameter("username");
    String pwd = request.getParameter("pwd");


    //新建cookie
    Cookie cook = new Cookie("username",user);
    Cookie cook2 = new Cookie("pwd",pwd);
    //设置有效期，秒为单位
    cook.setMaxAge(2*60);
    cook2.setMaxAge(2*60);
    //将cookie对象存放在response
    response.addCookie(cook);
    response.addCookie(cook2);

    Cookie[] cookies = request.getCookies();
    if (cookies!=null && cookies.length>0){
        //使用for循环取出cookie的值
        for (Cookie kin:cookies){
            if (kin.getName().equals("username")){
                out.print("用户名："+kin.getValue());
                out.print("<br />");
            }
            if (kin.getName().equals("pwd")){
                out.print("密码："+kin.getValue());
            }
        }
    }
%>
</body>
</html>
